package com.ruoyi.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.system.domain.SysProduct;
import com.ruoyi.system.mapper.SysProductMapper;
import com.ruoyi.system.query.ProductQuery;
import com.ruoyi.system.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.servlet.http.Part;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URL;
import java.util.List;
import java.util.UUID;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private SysProductMapper productMapper;
    @Override
    public void deleteById(Long id) {
            productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(String record, Part part) throws IOException {
        SysProduct sysProduct = new SysProduct();
        String[] arr = record.split("&");
        sysProduct.setCodingId(Long.valueOf(arr[0].split("=")[1]));
        sysProduct.setName(arr[1].split("=")[1]);
        sysProduct.setCostsPrice(new BigDecimal(arr[2].split("=")[1]));
        sysProduct.setSalePrice(new BigDecimal(arr[3].split("=")[1]));
        sysProduct.setBrand(arr[4].split("=")[1]);
        if (!"remark=".equals(arr[5])) {
            sysProduct.setRemark(arr[5].split("=")[1]);
        }
        File file = new File(ResourceUtils.getURL("classpath:").getPath());
        String property = System.getProperty("user.dir");
        String upload=property+"/ruoyi-admin/src/main/resources/static/upload";
        String random = UUID.randomUUID().toString().substring(0, 5);
        String substring = part.getSubmittedFileName().substring(part.getSubmittedFileName().lastIndexOf("."));
        if (!".png".equals(substring) && !".jpg".equals(substring)){
            throw new RuntimeException("请上传图片文件");
        }
        String name=random+substring;
        sysProduct.setImg(name);
        part.write(upload+"/"+random+substring);
           productMapper.insert(sysProduct);
    }

    @Override
    public SysProduct get(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<SysProduct> selectAll(ProductQuery qo) {
            PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo<>(productMapper.selectAllQuery(qo));
    }


    @Override
    public void update(String product, Part part) throws Exception {
        SysProduct sysProduct = new SysProduct();
        String[] arr = product.split("&");
        sysProduct.setId(Long.valueOf(arr[0].split("=")[1]));
        sysProduct.setCodingId(Long.valueOf(arr[1].split("=")[1]));
        sysProduct.setName(arr[2].split("=")[1]);
        sysProduct.setCostsPrice(new BigDecimal(arr[3].split("=")[1]));
        sysProduct.setSalePrice(new BigDecimal(arr[4].split("=")[1]));
        sysProduct.setBrand(arr[5].split("=")[1]);
        if (!"remark=".equals(arr[6])) {
            sysProduct.setRemark(arr[6].split("=")[1]);
        }
        File file = new File(ResourceUtils.getURL("classpath:").getPath());
        String property = System.getProperty("user.dir");
        String upload=property+"/ruoyi-admin/src/main/resources/static/upload";
        String random = UUID.randomUUID().toString().substring(0, 5);
        String substring = part.getSubmittedFileName().substring(part.getSubmittedFileName().lastIndexOf("."));
        if (!".png".equals(substring) && !".jpg".equals(substring)){
            throw new RuntimeException("请上传图片文件");
        }
        String name=random+substring;
        sysProduct.setImg(name);
        part.write(upload+"/"+random+substring);
        productMapper.updateByPrimaryKey(sysProduct);
    }

    @Override
    public List<SysProduct> select() {
        return productMapper.selectAll();
    }
}
